package net.chasing.androidbaseconfig.widget.rich.base;

import androidx.annotation.NonNull;

import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import net.chasing.androidbaseconfig.widget.rich.RichTextView;

/**
 * 超链接点击的事件处理器
 */
public class CustomUrlClickSpan extends ClickableSpan {
    private final String url;
    private final boolean enableClickUrl;
    private final int color;
    private OnUrlClickListener mOnUrlClickListener;

    public CustomUrlClickSpan(String url, boolean enableClickUrl, int color) {
        this.url = url;
        this.enableClickUrl = enableClickUrl;
        this.color = color;
    }

    public void setOnUrlClickListener(OnUrlClickListener onUrlClickListener) {
        mOnUrlClickListener = onUrlClickListener;
    }

    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
        super.updateDrawState(ds);
        ds.linkColor = color;
    }

    @Override
    public void onClick(@NonNull View widget) {
        if (RichTextView.IsLongClick) {
            RichTextView.IsLongClick = false;
            return;
        }
        // 超链接点击需要做的处理
        if (enableClickUrl && mOnUrlClickListener != null)
            mOnUrlClickListener.onClickUrl(url);
    }

    public interface OnUrlClickListener {
        void onClickUrl(String url);
    }
}